package com.tianhai.db.parser.ast;

import java.util.List;

/**
 * SELECT语句AST节点
 */
public class SelectStatement extends Statement {
    
    private final List<String> columns; // null表示SELECT *
    private final String tableName;
    private final WhereClause whereClause;
    private final OrderByClause orderByClause;
    private final Integer limit;
    
    public SelectStatement(List<String> columns, String tableName, 
                          WhereClause whereClause, OrderByClause orderByClause, Integer limit) {
        this.columns = columns;
        this.tableName = tableName;
        this.whereClause = whereClause;
        this.orderByClause = orderByClause;
        this.limit = limit;
    }
    
    @Override
    public StatementType getType() {
        return StatementType.SELECT;
    }
    
    public List<String> getColumns() {
        return columns;
    }
    
    public String getTableName() {
        return tableName;
    }
    
    public WhereClause getWhereClause() {
        return whereClause;
    }
    
    public OrderByClause getOrderByClause() {
        return orderByClause;
    }
    
    public Integer getLimit() {
        return limit;
    }
    
    public boolean isSelectAll() {
        return columns == null;
    }
    
    @Override
    public String toString() {
        return "SelectStatement{" +
                "columns=" + (columns == null ? "*" : columns) +
                ", tableName='" + tableName + '\'' +
                ", whereClause=" + whereClause +
                ", orderByClause=" + orderByClause +
                ", limit=" + limit +
                '}';
    }
    
    /**
     * WHERE子句
     */
    public static class WhereClause {
        private final List<Condition> conditions;
        
        public WhereClause(List<Condition> conditions) {
            this.conditions = conditions;
        }
        
        public List<Condition> getConditions() {
            return conditions;
        }
        
        @Override
        public String toString() {
            return "WHERE " + conditions;
        }
    }
    
    /**
     * 条件
     */
    public static class Condition {
        private final String columnName;
        private final Operator operator;
        private final Object value;
        
        public Condition(String columnName, Operator operator, Object value) {
            this.columnName = columnName;
            this.operator = operator;
            this.value = value;
        }
        
        public String getColumnName() {
            return columnName;
        }
        
        public Operator getOperator() {
            return operator;
        }
        
        public Object getValue() {
            return value;
        }
        
        @Override
        public String toString() {
            return columnName + " " + operator + " " + value;
        }
    }
    
    /**
     * 操作符
     */
    public enum Operator {
        EQUAL("="),
        NOT_EQUAL("!="),
        LESS_THAN("<"),
        GREATER_THAN(">"),
        LESS_THAN_OR_EQUAL("<="),
        GREATER_THAN_OR_EQUAL(">="),
        LIKE("LIKE");
        
        private final String symbol;
        
        Operator(String symbol) {
            this.symbol = symbol;
        }
        
        @Override
        public String toString() {
            return symbol;
        }
    }
    
    /**
     * ORDER BY子句
     */
    public static class OrderByClause {
        private final String columnName;
        private final boolean ascending;
        
        public OrderByClause(String columnName, boolean ascending) {
            this.columnName = columnName;
            this.ascending = ascending;
        }
        
        public String getColumnName() {
            return columnName;
        }
        
        public boolean isAscending() {
            return ascending;
        }
        
        @Override
        public String toString() {
            return "ORDER BY " + columnName + " " + (ascending ? "ASC" : "DESC");
        }
    }
}
